<template>
  <div class="energy-system-container">
    <div class="section">
      <h2 class="section-title">计算参数</h2>
      <div class="grid">
        <div class="card">
          <div class="card-content-wrapper">
            <!-- 规划年限 -->
            <div class="param-group">
              <label class="card-label">规划年限</label>
              <div class="input-group">
                <input
                  type="number"
                  :value="environment.carbonPrice"
                  @input="handleCarbonPriceInput"
                  class="input small"
                  min="5"
                  max="100"
                />
                <span class="unit">(N≥5)</span>
                <span v-if="showMaxWarning" class="error-message">输入值不能小于5</span>
              </div>
            </div>

            <!-- 是否考虑早期淘汰 -->
            <div class="param-group">
              <label class="card-label">是否考虑早期设备淘汰</label>
              <el-radio-group v-model="considerEarlyReplacement" class="radio-group">
                <el-radio label="0" class="radio-item">是</el-radio>
                <el-radio label="1" class="radio-item">否</el-radio>
              </el-radio-group>
            </div>

            <!-- 规划方式 -->
            <div class="param-group">
              <label class="card-label">规划方式</label>
              <el-radio-group v-model="planningMethod" @change="handlePlanningMethodChange" class="radio-group">
                <el-radio :label="0" class="radio-item">单阶段规划</el-radio>
                <el-radio :label="1" class="radio-item">多阶段规划</el-radio>
                <el-radio :label="2" class="radio-item">动态多阶段规划</el-radio>
              </el-radio-group>
            </div>

            <!-- 每阶段周期 -->
            <div v-if="showPhasePeriod" class="param-group mt-20">
              <label class="card-label">每阶段周期(年)</label>
              <div class="input-group">
                <el-input
                  v-model.number="phasePeriod"
                  type="number"
                  :disabled="!isPhasePeriodEditable"
                  class="input phase-period-input"
                  placeholder="输入阶段周期"
                />
              </div>
            </div>

            <!-- 提交按钮 -->
            <button class="distribution-btn emission-submit-btn" @click="submitAllDeviceParams">
              <span class="btn-text">提交设备参数</span>

            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

// 环境参数
const environment = ref({
  carbonPrice: 5,
  carbonLimit: 1000
});

const showMaxWarning = ref(false);
const considerEarlyReplacement = ref('否');
const planningMethod = ref(1);
const phasePeriod = ref(1);

// 是否显示阶段周期
const showPhasePeriod = computed(() => {
  return planningMethod.value !== 1;
});

// 是否可编辑
const isPhasePeriodEditable = computed(() => planningMethod.value === 2);

// 规划方式变更处理
const handlePlanningMethodChange = (value: number) => {
  if (value === 1 || value === 3) {
    phasePeriod.value = 1;
  }
};

// 限制规划年限输入
const handleCarbonPriceInput = (event: Event) => {
  const inputElement = event.target as HTMLInputElement;
  let value = parseFloat(inputElement.value);

  if (value < 5) {
    value = 5;
    showMaxWarning.value = true;
  } else {
    showMaxWarning.value = false;
  }

  environment.value.carbonPrice = value;
  inputElement.value = value.toString();
};

// 示例提交函数
const submitAllDeviceParams = async () => {
  try {
    const value = environment.value.carbonPrice;
    const die =  considerEarlyReplacement.value;
    const way = planningMethod.value;
    const response = await axios.post(`http://localhost:8080/planc/insert/${value}/${die}/${way}`);
    if(response.data.code === 0){
      ElMessage.success('设备参数提交成功');
    }else{
      ElMessage.success(response.data.message);
    }
  } catch (err) {
    console.error(err);
    ElMessage.error('参数提交失败，请检查网络');
  }
};
</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.energy-system-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.section-title {
  color: #4c1d95;
  margin-bottom: 24px;
  font-size: 24px;
  font-weight: 700;
  padding-bottom: 12px;
  border-bottom: 2px solid #7c3aed;
}

.section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.card {
  background-color: white;
  border-radius: 8px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  padding: 24px;
}

.card-content-wrapper {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.param-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input {
  padding: 8px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  background: white;
  color: #1e293b;
  transition: border-color 0.3s;
}

.input.small {
  width: 80px;
}

.input.phase-period-input {
  width: 120px;
}

.input:focus {
  outline: none;
  border-color: #7c3aed;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.1);
}

.unit {
  font-weight: 500;
  color: #64748b;
  font-size: 13px;
  white-space: nowrap;
}

.error-message {
  color: #ef4444;
  font-size: 12px;
  margin-left: 8px;
}

.radio-group {
  display: flex;
  gap: 16px;
}

.radio-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #64748b;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.radio-item:hover {
  background-color: #f8fafc;
}

.el-radio.is-checked .radio-item {
  color: #7c3aed;
  font-weight: 500;
  background-color: #f8fafc;
}

.mt-20 {
  margin-top: 20px;
}
</style>
